<?php

namespace Scripts;

require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/../bootstrap/database.php';

use App\Models\YikaoMajorLevel;
use App\Workers\YikaoSpecialDetailSpiderWorker;
use Workerman\Worker;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$baseUrl = dirname(__DIR__).'/../college';

$filePath = dirname(__DIR__) . '/public/data/majors_handle.json';

$content = file_get_contents($filePath);

$majorLevel = json_decode($content, true);

$data = [];
foreach ($majorLevel as $majors) {
    $data[] = [
        'name' => $majors['name'],
        'level1' => $majors['level1'],
        'level2' => 0,
        'spe_id' => $majors['spe_id'],
        'code' => $majors['code'],
        'parent_id' => 0,
        'num' => $majors['num'] ?? 0
    ];
    foreach ($majors['subcategories'] as $key => $level1) {
        foreach ($level1 as $level2) {
            $data[] = [
                'name' => $level2['name'],
                'level1' => $level2['level1'],
                'level2' => in_array($key, ['1', '230', '2']) ? 0 : $key,
                'spe_id' => $level2['spe_id'],
                'code' => $level2['code'],
                'parent_id' => in_array($key, ['1', '230', '2']) ? 1 : 2,
                'num' => $level2['num'] ?? 0
            ];

        }
    }
}

$res = YikaoMajorLevel::query()->insert($data);
print_r($res);






